package h.framework.pacy.utils;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;

/**
 * 创建人 PACY
 * 创建时间 18/7/11 14:25.
 * 邮箱 pengwwe457621023@qq.com
 * 类描述  网络状态判断
 */

public class NetWorkUtils {
    /**
     * 判断当前是否有网络连接
     *
     * @param context
     * @return 有网络返回true；无网络返回false
     */
    @SuppressWarnings("null")
    public static boolean isNetWorkEnable(Context context) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (manager != null) {
            /**
             * 获取网络信息实体
             * 由于从系统服务中获取数据属于进程间通信，基本类型外的数据必须实现Parcelable接口，
             * NetworkInfo实现了Parcelable，获取到的activeNetInfo相当于服务中网络信息实体对象的一个副本（拷贝），
             * 所以，不管系统网络服务中的实体对象是否置为了null，此处获得的activeNetInfo均不会发生变化
             */
            NetworkInfo activeNetInfo = manager.getActiveNetworkInfo();
            if (activeNetInfo != null) {
                return activeNetInfo.isAvailable();
            }
        }
        return false;
    }

        /**
         * 判断当前网络是否为wifi
         *
         * @param context
         * @return 如果为wifi返回true；否则返回false
         */
        @SuppressWarnings("static-access")
        public static boolean isWiFiConnected (Context context){
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();
            return networkInfo.getType() == manager.TYPE_WIFI ? true : false;
        }

        /**
         * 判断MOBILE网络是否可用
         *
         * @param context
         * @return
         * @throws Exception
         */
        public static boolean isMobileDataEnable (Context context){
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            boolean isMobileDataEnable = false;
            isMobileDataEnable = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
            return isMobileDataEnable;
        }

        /**
         * 判断wifi 是否可用
         *
         * @param context
         * @return
         * @throws Exception
         */
        public static boolean isWifiDataEnable (Context context){
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            boolean isWifiDataEnable = false;
            isWifiDataEnable = manager.getNetworkInfo(
                ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
            return isWifiDataEnable;
        }

        /**
         * 跳转到网络设置页面
         *
         * @param activity
         */
        public static void GoSetting (Activity activity){
            Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
            activity.startActivity(intent);
        }

        /**
         * 打开网络设置界面
         */
        public static void openSetting (Activity activity){
            Intent intent = new Intent("/");
            ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
            intent.setComponent(cn);
            intent.setAction("android.intent.action.VIEW");
            activity.startActivityForResult(intent, 0);
        }

    }
